package com.demo.common.kit;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;


public class TimeKit {

	private static final Log log = Log.getLog(TimeKit.class);
	
	public static Calendar getStartOverTimeCal(Date worktime) {
		Calendar now = Calendar.getInstance();
		now.setTime(worktime);
		if(now.get(Calendar.HOUR_OF_DAY) < 19) {
			now.add(Calendar.DATE, -1);
		}
		return now;
	}
	
	/**
	 * 获取当天的加班开始时间
	 * */
	public static String getStartOverTime(Date worktime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 19:00:00");
		return sdf.format(getStartOverTimeCal(worktime).getTime());
	}
	
	/**
	 * 获取当天加班截止时间
	 * */
	public static String getEndOverTime(Date worktime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 08:30:00");
		Calendar now = Calendar.getInstance();
		now.setTime(worktime);
		if(now.get(Calendar.HOUR_OF_DAY) > 19) {
			now.add(Calendar.DATE, 1);
		}
		return sdf.format(now.getTime());
	}
	
	/**
	 * 计算加班的时间
	 * */
	public static BigDecimal countOverTime(Date worktime) {
		Calendar start = getStartOverTimeCal(worktime);
		start.set(Calendar.HOUR_OF_DAY, 19);
		start.set(Calendar.MINUTE, 0);
		start.set(Calendar.SECOND, 0);
		Calendar end = Calendar.getInstance();
		end.setTime(worktime);
		return getHalfhourAccount(start, end);
	}
	
	public static BigDecimal getHalfhourAccount(Calendar startTime, Calendar endTime) {
		long tmp = (endTime.getTimeInMillis() - startTime.getTimeInMillis()) / (1000*60*30);
		return BigDecimal.valueOf(tmp).divide(BigDecimal.valueOf(2),1, BigDecimal.ROUND_HALF_UP);
	}
	
	/**
	 * 计算周末上班时间
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static BigDecimal countOverTimeWeekEnd(Date startTime, Date endTime) {
		long workTime = endTime.getTime() - startTime.getTime();
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(startTime);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(endTime);
		int startHour = startCalendar.get(Calendar.HOUR_OF_DAY);
		int endHour = endCalendar.get(Calendar.HOUR_OF_DAY);
		if((startHour <= 13 && endHour >= 13 && endHour <= 19) || (startHour >=12 && startHour <= 19 && endHour >= 19)) {//如果上班在上午，下班在下午
			workTime -= (90 * 60 * 1000);
		} else if(startHour <= 11 && endHour >= 19) {//如果上班在上午，下班在晚上
			workTime -= (180 * 60 * 1000);
		}
		return BigDecimal.valueOf(workTime / (1000*60*30)).divide(BigDecimal.valueOf(2),1, BigDecimal.ROUND_HALF_UP);
	}
	
	/**
	 * 判断是否是周末
	 * @return
	 */
	public static boolean isWeekend() {
		return isWeekend(Calendar.getInstance());
	}
	
	/**
	 * 判断是否是周末
	 * @return
	 */
	public static boolean isWeekend(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return isWeekend(calendar);
	}
	
	/**
	 * 判断是否是周末
	 * @return
	 */
	public static boolean isWeekend(Calendar calendar) {
		if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || Calendar.getInstance().get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
			return true;
		return false;
	}
	
	public static Date getDateByFormat(Date date, String format) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(sdf.format(date));
	}
	
	public static int getDayOfMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.DATE);
	}
	
	
	public static void main(String[] args) {
		System.out.println(getDayOfMonth(new Date()));
	}

	public static Calendar stringToCalendar(String startTime) {
		if(!StrKit.isBlank(startTime)) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(sdf.parse(startTime));
				return calendar;
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
	
}
